<template>
    <div class="app-container">
      <el-row :gutter="20">
  
        <el-col>
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="料仓名称" prop="siloStockName">
              <el-input
                v-model="queryParams.siloStockName"
                placeholder="请输入料仓名称"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="dateRange"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
  
          <el-row :gutter="10" class="mb8">
            <!-- <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['radarSilo:split:add']"
              >新增</el-button>
            </el-col> -->
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['radarSilo:split:update']"
              >修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['radarSilo:split:remove']"
              >删除</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
              <el-button
                type="info"
                plain
                icon="el-icon-upload2"
                size="mini"
                @click="handleImport"
                v-hasPermi="['radarSilo:result:import']"
              >导入</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPermi="['radarSilo:result:export']"
              >导出</el-button>
            </el-col> -->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
          </el-row>
  
          <el-table border v-loading="loading" :data="siloSplitList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="分割id" align="center" key="resultId" prop="resultId" v-if="columns[0].visible" :show-overflow-tooltip="true"/>
            <el-table-column label="料仓名称" align="center" key="siloStockName" prop="siloStockName" v-if="columns[1].visible" :show-overflow-tooltip="true"/>
            <el-table-column label="体积" align="center" key="splitVol" prop="splitVol" v-if="columns[2].visible" :show-overflow-tooltip="true"/>
            <el-table-column label="平均物位" align="center" key="splitLev" prop="splitLev" v-if="columns[3].visible" :show-overflow-tooltip="true"/>
            <el-table-column label="最高物位" align="center" key="splitMax" prop="splitMax" v-if="columns[4].visible" :show-overflow-tooltip="true"/>
            <el-table-column label="最低物位" align="center" key="splitMin" prop="splitMin" v-if="columns[5].visible" :show-overflow-tooltip="true"/>
            <el-table-column label="料堆密度" align="center" key="coalData.coalPileDensity" prop="coalData.coalPileDensity" v-if="columns[6].visible" :show-overflow-tooltip="true"/>
            <el-table-column label="料堆基准面" align="center" key="coalData.coalPileGround" prop="coalData.coalPileGround" v-if="columns[7].visible" :show-overflow-tooltip="true"/>
            <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[8].visible" width="160" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              width="160"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['radarSilo:silo:update']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['radarSilo:silo:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
  
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-row>
  
      <!-- 添加或修改用户配置对话框 -->
      <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="600px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col>
              <el-form-item label="料仓名称" prop="siloStockName">
                <el-select v-model="form.siloStockId" placeholder="请选择料仓">
                  <el-option
                      v-for="item in siloList"
                      :key="item.siloStockId"
                      :label="item.siloStockName"
                      :value="item.siloStockId">
                  </el-option>
              </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="料堆密度" prop="coalData.coalPileDensity">
                <el-input v-model="form.coalData.coalPileDensity" placeholder="请输入料堆密度" maxlength="30" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="料堆基准面" prop="coalData.coalPileGround">
                <el-input v-model="form.coalData.coalPileGround" placeholder="请输入体积" maxlength="30" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
  
  
      <!-- 设备导入对话框 -->
      <el-dialog :close-on-click-modal="false" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
        <el-upload
          ref="upload"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url + '?updateSupport=' + upload.updateSupport"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip text-center" slot="tip">
            <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的设备数据
            </div>
            <span>仅允许导入xls、xlsx格式文件。</span>
            <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
          </div>
        </el-upload>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
import {selectSiloSplitList,addSiloSplit,delSplit} from '@/api/radar/radarSplit';
  import { getToken } from "@/utils/auth";
  
  import * as echarts from "echarts";
  import moment from 'moment'
  
  export default {
    name: "silo",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        selectList: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 历史数据表格数据
        siloSplitList: null,
        siloList:undefined,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 日期范围
        dateRange: [],
        // 表单参数
        form: {
          coalData:{
            coalPileDensity:0,
            coalPileGround:0,
          }
        },
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false, 
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/radarResult/importData"
        }, 
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          type: 1,
          siloStockName: undefined,
          startTime: undefined,
          endTime: undefined
        },
        // 列信息
        columns: [
          { key: 0, label: `分割id`, visible: true },
          { key: 1, label: `料仓名称`, visible: true },
          { key: 2, label: `体积`, visible: true },
          { key: 3, label: `平均物位`, visible: true },
          { key: 4, label: `最高物位`, visible: true },
          { key: 5, label: `最低物位`, visible: true },
          { key: 6, label: `料堆密度`, visible: true },
          { key: 7, label: `料堆基准面`, visible: true },
          { key: 8, label: `创建时间`, visible: true }
        ],

        // 表单校验
        rules: {
          // equipName: [
          //   { required: true, message: "设备名称不能为空", trigger: "blur" },
          //   { min: 0, max: 20, message: '设备名称长度必须介于 0 和 20 之间', trigger: 'blur' }
          // ],
        }
      };
    },
    watch: {
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询数据列表 */
      getList() {
        this.loading = true;
        if(this.dateRange.length > 0){
          this.queryParams.startTime = this.dateRange[0];
          this.queryParams.endTime = this.dateRange[1];
        } 
        selectSiloSplitList(this.queryParams).then(res => {
            this.siloSplitList = res.data.data;
            var coalList = res.data.coalData.list;
            for(var i = 0;i < this.siloSplitList.length;i++){
              var index = coalList.filter(item => item.id === this.siloSplitList[i].splitId);
              // console.log("?????????????",index)
              if(index.length >= 1){
                this.siloSplitList[i].coalData = index[0]
              }else{
                this.siloSplitList[i].coalData = {};
              }
            }
            this.total = res.data.total;
            this.loading = false;
          }
        );
      },
  
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          coalData:{
            coalPileDensity:0,
            coalPileGround:0
          }
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.queryParams.startTime=undefined;
        this.queryParams.endTime=undefined;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.selectList = selection;
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      // handleAdd() {
      //   this.reset();
      //   this.open = true;
      //   this.title = "添加料仓";
      // },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form = JSON.parse(JSON.stringify(this.selectList[0] || row))
        this.open = true;
        this.title = "修改参数";
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.equipId != undefined) {
              updateSiloResult(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
            //  else {
            //   addSiloResult(this.form).then(response => {
            //     this.$modal.msgSuccess("新增成功");
            //     this.open = false;
            //     this.getList();
            //   });
            // }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const splitIds = row.splitId || this.selectList.map(item => item.splitId);
        this.$modal.confirm('是否确认删除编号为"' + splitIds + '"的数据项？').then(function() {
          return delSplit(splitIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('radarResult/export', {
          ...this.queryParams
        }, `equip_${new Date().getTime()}.xlsx`)
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "历史数据导入";
        this.upload.open = true;
      },
      /** 下载模板操作 */
      importTemplate() {
        this.download('radarResult/importTemplate', {
        }, `radar_result_${new Date().getTime()}.xlsx`)
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      },
      
    }
  };
  </script>
  
  <style lang="scss" scoped>
  .his-chart {
    width: 550px;
    height:500px;
  }
  #imageMap{
    height: 500px;
  }
  
  </style>